﻿
Ext.namespace('FantastPlatform', 'FantastPlatform.UI');

var HouseDistrictStore = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: '../Service/HousePersonAddService.aspx?Action=GetHouseDistrict'
    }),
    reader: new Ext.data.JsonReader({
        root: 'topics',
        totalProperty: 'totalCount',
        id: 'Name',
        fields: ['Name', 'Id']
    }),
    remoteStore: true
});
HouseDistrictStore.load();

var PlaceTypeStore = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: '../Service/HousePersonAddService.aspx?Action=GetPlaceType'
    }),
    reader: new Ext.data.JsonReader({
        root: 'topics',
        totalProperty: 'totalCount',
        id: 'Name',
        fields: ['Name', 'Id']
    }),
    remoteStore: true
});
PlaceTypeStore.load();

FantastPlatform.UI.HousePersonAddForm = function (config) {
    Ext.apply(this, config);
    Ext.override(Ext.form.CheckboxGroup, {
        //在inputValue中找到定义的内容后，设置到items里的各个checkbox中    
        setValue: function (value) {
            this.items.each(function (f) {
                if (value.indexOf(f.inputValue) != -1) {
                    f.setValue(true);
                } else {
                    f.setValue(false);
                }

            });
        },
        //以value1,value2的形式拼接group内的值   
        getValue: function () {
            var re = "";
            this.items.each(function (f) {
                if (f.getValue() == true) {
                    re += f.inputValue + ",";
                }
            });
            return re.substr(0, re.length - 1);
        },
        //在Field类中定义的getName方法不符合CheckBoxGroup中默认的定义，因此需要重写该方法使其可以被BasicForm找到   
        getName: function () {
            return this.name;
        }
    });
    var count2 = purPoseStore.getCount();
    var pCheckboxItems = [];
    for (var i = 0; i < count2; i++) {
        var boxLabel = purPoseStore.getAt(i).get("Name");
        var name = purPoseStore.getAt(i).get("Id");
        var value = purPoseStore.getAt(i).get("Code");
        pCheckboxItems.push({
            boxLabel: boxLabel,
            name: 'purpose' + name,
            value: value,
            inputValue: name

        });
    }
    var purPoseCheckboxGroup = new Ext.form.CheckboxGroup({
        fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;房物用途',
        name: 'Purpose',
        hiddenName: 'Purpose',
        displayField: 'Name',
        mode: 'remote',
        valueField: 'Id',
        allowBlank: false,
        editable: false,
        triggerAction: 'all',
        emptyText: '选择房屋用途',
        columns: 2,
        items: pCheckboxItems
    });
    var HouseDistrictComboBox = new Ext.form.ComboBox({
        fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;所属区域',
        name: 'HouseDistrict',
        hiddenName: 'HouseDistrict',
        store: HouseDistrictStore,
        displayField: 'Name',
        mode: 'remote',
        valueField: 'Id',
        editable: false,
        triggerAction: 'all',
        emptyText: '选择所属区域',
        width: 340
    });
    this.items = [{
        layout: 'column',
        border: false,
        labelSeparator: '：',
        items: [{
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.Hidden({
                name: 'Enable'
            })]
        }, {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.Hidden({
                name: 'CreateDate'
            })]
        }, {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '门牌号',
                name: 'Number',
                allowBlank: false,
                blankText: "门牌号不能为空",
                anchor: '98%'
            })]
        }, {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [HouseDistrictComboBox],
            anchor: '98%'
        },
        {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new FantastPlatform.UI.DateField({
                fieldLabel: '租赁到期时间',
                name: 'Deadline',
                format: 'Y-m-d',
                width: 105,
                dateFormat: 'Y-m-d'
            })],
            anchor: '98%'
        },
         {
             columnWidth: 1,
             border: false,
             layout: 'form',
             items: [purPoseCheckboxGroup],
             anchor: '98%'
         }]
    }];

    FantastPlatform.UI.HousePersonAddForm.superclass.constructor.call(this, {
        bodyStyle: 'padding:5px',
        collapsible: true,
        labelAlign: 'right',
        autoScroll: true,
        border: false
    });
};

Ext.extend(FantastPlatform.UI.HousePersonAddForm, Ext.form.FormPanel, {
    onRender: function (ct, position) {
        FantastPlatform.UI.HousePersonAddForm.superclass.onRender.call(this, ct, position);
        var houseForm = this.form;
        if (this.HouseHoldId && this.HouseHoldId != 0) {
            this.add(new Ext.form.Hidden({ name: 'Id', value: this.HouseHoldId }));
            this.form.load({
                url: '../Service/HousePersonAddService.aspx?Action=Get',
                params: { id: this.HouseHoldId },
                waitMsg: '正在读取数据...',
                waitTitle: '请稍后...',
                failure: function (form, action) {
                    if (action.failureType != 'client') {
                        showErrorMessage('操作失败', action.result.message, '');
                    }
                }
            });
            Ext.Ajax.request({
                method: 'post',
                url: "../Service/HousePersonAddService.aspx?Action=GetPurposeByHouseId",
                params: { id: this.HouseHoldId },
                success: function (request, options) {
                    // 服务器返回json形式的结果                                          
                    var reText = request.responseText;
                    var reTextObj = Ext.util.JSON.decode(reText);
                    if (eval(reTextObj.success)) {
                        houseForm.findField('Purpose').setValue(reTextObj.message);
                    }
                }
            });
        }
    }
});

